<?php
	/**
	 * @copyright (C)2016-2099 Hnaoyun Inc.
	 * @author XingMeng
	 * @email hnxsh@foxmail.com
	 * @date 2016年11月6日
	 *  应用控制基类
	 */

	/**
	 * json  0失败  1成功  2跳转  3弹窗   4未激活  5冻结 6页面显示错误
	 */

	namespace core\basic;

	use core\view\Paging;
	use core\view\View;

	class Controller
	{


		final protected function CheckUser()
		{
			// 未登录时跳转到用户登录
			if (! session('uid')) {
				json(2, '前往登录！', 'sign-in.html');
			}
			$memcacheInfo = $this->Memcache(2,session('uid'));

			if ($memcacheInfo->id != session('uid')) {
				json(2, '操作有误！', 'sign-in.html');
			}
			if (!$memcacheInfo->id) {
				json(2, '登录超时！', 'sign-in.html');
			}
			$userInfo = model('admin.member.Member')->checkMember(['id' => $memcacheInfo->id]);
			if ($userInfo->status != 1) {

				$this->Memcache(4, session('uid'));
				json(5, '账号已经冻结', 'sign-in.html');
			}

		}


		// 显示模板
		final protected function display($file)
		{
			$view = View::getInstance();
			$content = $view->parser($file);
			$content = $this->runtime($content);
			echo $this->gzip($content);
			exit();
		}

		/**
		 * @param $status 1 添加 2获取 3 删除全部  4 删除单个
		 * @param $key
		 * @param $var
		 * @param $time
		 * @return array|bool|string
		 */
		final  protected function Memcache($status, $key, $var, $time)
		{
			$memcache = new \Memcache();
			$memcache->connect('127.0.0.1', 11211) or json(0, 'Memcache未开启！');
			switch ($status) {
				case 1;
					return $memcache->set($key, $var, 0, $time);
					break;
				case 2;
					return $memcache->get($key);
					break;
				case 3;
					return $memcache->flush();
					break;
				case 4;
					return $memcache->delete($key);
					break;
				default:

			}
		}

		// 解析模板
		final protected function parser($file)
		{
			$view = View::getInstance();
			return $view->parser($file);
		}

		// 缓存页面内容,默认直接显示内容，可传递第二参数false返回内容
		final protected function cache($content, $display = true)
		{
			$view = View::getInstance();
			if (Config::get('tpl_html_cache')) {
				$content = str_replace('{pboot:runtime}', 'Cached at ' . date('Y-m-d H:i:s'), $content);
			} else {
				$content = $this->runtime($content);
			}
			$view->cache($content); // 压缩前缓存
			$content = $this->gzip($content);
			if ($display) {
				echo $content;
				exit();
			} else {
				return $content;
			}
		}

		// 设置视图主题
		final protected function setTheme($themeName)
		{
			$view = View::getInstance();
			$view->assign('theme', $themeName);
		}

		// 变量注入接口
		final protected function assign($var, $value)
		{
			$view = View::getInstance();
			$view->assign($var, $value);
		}

		// 变量获取接口
		final protected function getVar($var)
		{
			$view = View::getInstance();
			return $view->getVar($var);
		}

		// 手动生成分页信息,返回限制语句
		final protected function page($tatal, $morePageStr = false)
		{
			$page = Paging::getInstance();
			return $page->limit($tatal, $morePageStr);
		}

		// 获取配置信息
		final protected function config($item = null, $array = false)
		{
			return Config::get($item, $array);
		}

		// 缓存配置信息
		final protected function setConfig($itemName, array $data)
		{
			return Config::set($itemName, $data);
		}

		// 写入日志信息
		final protected function log($content, $level = "info")
		{
			Log::write($content, $level);
		}

		// 解析运行时间标签
		private function runtime($content)
		{
			return str_replace('{pboot:runtime}', 'Processed in ' . round(microtime(true) - START_TIME, 6) . ' second(s).', $content);
		}

		// 压缩内容
		private function gzip($content)
		{
			if (Config::get('gzip') && !headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) {
				$content = gzencode($content, 6);
				header("Content-Encoding: gzip");
				header("Vary: Accept-Encoding");
				header("Content-Length: " . strlen($content));
			}
			return $content;
		}
	}

